package com.cloudknows.data.qa.zhidao;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Zhidao {
	public String id = null;
	public String field = null;
	public String question = null;
	public String description = null;
	public List<String> answers = null;

	final static Pattern ID_PATTERN = Pattern
			.compile("__Qid__:(.*)\n[\\s\\S]*");
	final static Pattern FIELD_PATTERN = Pattern
			.compile("[\\s\\S]*__QField__:(.*)\n[\\s\\S]*");
	final static Pattern QUESTION_PATTERN = Pattern
			.compile("[\\s\\S]*__QTitle__:(.*)\n[\\s\\S]*");
	final static Pattern ANSWER_LIST_PATTERN = Pattern
			.compile("[\\s\\S]*__Answer__:\n([\\s\\S]*)\n$");

	public Zhidao(String zhidaoStr) {
		Matcher m = ID_PATTERN.matcher(zhidaoStr);
		if (m.matches()) {
			this.id = m.group(1);
		}
		m = FIELD_PATTERN.matcher(zhidaoStr);
		if (m.matches()) {
			this.field = m.group(1);
		}
		m = QUESTION_PATTERN.matcher(zhidaoStr);
		if (m.matches()) {
			this.question = m.group(1);
		}
		m = ANSWER_LIST_PATTERN.matcher(zhidaoStr);
		if (m.matches()) {
			this.answers = new ArrayList<String>();
			final String anwserStrs = m.group(1);
			int start = 0;
			final String prefixStr = "	__ASeq__";
			while (start < anwserStrs.length()) {

				int idx = anwserStrs.indexOf(prefixStr, start);
				if (-1 == idx)
					break;
				int answerBeg = anwserStrs.indexOf(":", idx) + 1;
				idx = anwserStrs.indexOf("	__ASeq__", answerBeg);
				int answerEnd = (-1 != idx) ? idx : anwserStrs.length();
				this.answers.add(anwserStrs.substring(answerBeg, answerEnd));
				start = answerEnd;
			}
		}

	}

	public String getId() {
		return id;
	}

	public String getField() {
		return field;
	}

	public String getQuestion() {
		return question;
	}

	public List<String> getAnswers() {
		return answers;
	}

	public static void main(String[] args) throws Exception {
		String example = "__Qid__:277471733\n__QField__:百度知道 > 教育/科学 > 院校信息\n__QTitle__:大学专业选金融好，还是选经济好\n__QDescription__:\n我是女生，理科还行，数学也还不错，其实总得来说偏理科一些，大学想学经济类的，不知道学经济好还是金融好。这两个有什么区别。如果只读大学4年的话，是不是应该继续深造呢。\n__Answer__:\n	__ASeq__1__:个人建议还是学经济比较好，学经济方面广，不像金融比较。。再说经济就业也比金融好吧 。。除非你银行单位有人。。。。。\n	__ASeq__2__:金融吧\n	__ASeq__3__:经济师什么都学一点，什么都不清楚，主要是为当老师和做研究准备的专业。金融专业性强，学的好去外资银行，钞票刚刚的，力挺金融。不过学经济可以考公务员\n	__ASeq__4__:金融。\n";
		FileReader reader = new FileReader(
				"../cloud-knows/data/zhidao/zhidao_sample.txt");
		char[] buff = new char[1024000];
		String line = "";
		final String prefix = "__Qid__:";
		final String surfix = "\n__Qid__:";
		
		int beg = 0;
		int end = 0;
		int len = 0;
		while (-1 != (len = (reader.read(buff, 0, buff.length)))) {
			line += new String(buff, 0, len);
			while (beg < line.length()) {

				beg = line.indexOf(prefix, beg);
				if (-1 == beg) break;
				end = line.indexOf(surfix, beg + prefix.length());
				if (-1 == end) break;

				String zhidaoStr = line.substring(beg, end);
				Zhidao zhidao = new Zhidao(zhidaoStr);

				System.out.println("ID:" + zhidao.getId());
				System.out.println("FIELD:" + zhidao.getField());
				System.out.println("Q:" + zhidao.getQuestion());
				System.out.println("A:" + zhidao.getAnswers());
				beg = end;
			}
			if (-1 == beg) beg = 0;
			line = line.substring(beg);
			beg = 0;
			end = 0;
			
		}
		
		Zhidao zhidao = new Zhidao(line);

		System.out.println("ID:" + zhidao.getId());
		System.out.println("FIELD:" + zhidao.getField());
		System.out.println("Q:" + zhidao.getQuestion());
		System.out.println("A:" + zhidao.getAnswers());
		
		
		
		
	}
}
